package com.dims_hsrc.web.controller.data;

import com.dims_hsrc.common.annotation.Log;
import com.dims_hsrc.common.core.controller.BaseController;
import com.dims_hsrc.common.core.domain.AjaxResult;
import com.dims_hsrc.common.enums.BusinessType;
import com.dims_hsrc.common.utils.poi.ExcelUtil;
import com.dims_hsrc.data.domain.DataDemand;
import com.dims_hsrc.data.service.IDataDemandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 高速列车需求Controller
 *
 * @author 赵浩然
 * @date 2025-03-17
 */
@RestController
@RequestMapping("/data/demand")
public class DataDemandController extends BaseController {
    @Autowired
    private IDataDemandService serv;

    /**
     * 查询需求列表
     */
    @GetMapping("/list")
    public AjaxResult list(DataDemand demand) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataDemand> list = serv.list(demand);
        return success(list);
    }

    /**
     * 导出需求列表
     */
    @Log(title = "需求", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse resp, DataDemand demand) {
        System.out.println(new Throwable().getStackTrace()[0]);
        List<DataDemand> list = serv.list(demand);
        ExcelUtil<DataDemand> util = new ExcelUtil<DataDemand>(DataDemand.class);
        util.exportExcel(resp, list, "高速列车需求数据");
    }

    /**
     * 获取需求详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return success(serv.getOne(id));
    }

    /**
     * 新增高速列车需求
     */
    @Log(title = "高速列车需求", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataDemand demand) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(serv.add(demand));
    }

    /**
     * 修改高速列车需求
     */
    @Log(title = "高速列车需求", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult update(@RequestBody DataDemand demand) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(serv.update(demand));
    }

    /**
     * 删除需求
     */
    @Log(title = "高速列车需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        System.out.println(new Throwable().getStackTrace()[0]);
        return toAjax(serv.remove(ids));
    }
}
